<?
/**
 * File contains Views Counter core
 * 
 * VC's core is designed to be extendable for specific purposes (e.g. for to act as Wordpress plugin)
 * 
 * @package	VC
 * @author	cryonyx
 */

//	We need logger class plugged in so that logger functionality works fine 
require_once(dirname(__FILE__) . "/vc.logger.class.php");


/**
 * Class vc_core is a Views Counter's core containing its main functionality
 */
abstract class vc_core {
	/**
	 * @var			array
	 * @var			boolean
	 * @staticvar	string
	 */
	protected	$statistics;
	protected	$autosave;
	const		VC_VERSION	=	'1.0';
	
	/**
	 * Class constructor
	 *
	 * @param	boolean	$autosave
	 * @return	vc_core
	 */
	public function __construct($autosave = FALSE) {
		$this->autosave	=	$autosave;
	}
	
	/**
	 * Increases counter for specified ID and returns data array for it
	 *
	 * @param integer	$id
	 * @param string	$ip
	 */
	public function increaseCounter($id, $ip) {
		if(isset($this->statistics[$id])) {
			//	Increase counter for this entry and log visitor's IP
			$this->statistics[$id]['views']			=	intval($this->statistics[$id]['views']) + 1;
			
			//	Check whether this visitor has already viewed this page. If not, add him
			if(!in_array($ip_c, $this->statistics[$id]['visitors'])) {
				$this->statistics[$id]['visitors'][]	=	$ip;
			}
		} else {
			//	If this entry was not tracked before, initialize it
			$this->statistics[$id]['views']			=	1;
			$this->statistics[$id]['visitors'][]	=	$ip;
		}
		
		//	If user decided to auto-save data on any change, then do it
		if($this->autosave) {
			self::saveData();
		}
	}
	
	/**
	 * Returns statistical data for specified entry
	 *
	 * @param	integer	$id
	 * @return	array|NULL
	 */
	public function getStatistics($id) {
		if(	!isset($id)		||
			!isset($this->statistics[$id])
		) {
			return NULL;
		}
		
		return array(
			'views'		=>	$this->statistics[$id]['views'],
			'visitors'	=>	count($this->statistics[$id]['visitors'])
		);
	}
	
	/**
	 * Returns version of VC core
	 *
	 * @return string
	 */
	public function getVersion() {
		return self::VC_VERSION;
	}
	
	/**
	 * Saves statistics to database
	 * 
	 * This is to be performed using purpose-specific objects for work with database
	 */
	abstract public function saveData();
	
	/**
	 * Loads statistics from database
	 * 
	 * This is to be performed using purpose-specific objects for work with database
	 */
	abstract public function loadData();
	
	/**
	 * Logs a message using a specific log driver
	 *
	 * @param string	$message	Message to log
	 * @param integer	$status		Message severity
	 * @param string	$place		Where event has occured
	 */
	abstract private function logThis($message, $status, $place);
	
	/**
	 * Generates a URL pointing at VC's logo image
	 */
	abstract public function getFunnyPictureURL();
	
	/**
	 * Returns standard <img> tag pointing at VC's logo image
	 *
	 */
	abstract public function getFunnyPicture();
}
?>